package com.jtech.together.manager;

import com.jtech.together.model.UserModel;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.v3.BmobUser;

/**
 * 用户管理
 * Created by wuxubaiyang on 2017/5/10.
 */

public class UserManager {
    /**
     * 判断当前是否已有用户登录
     *
     * @return
     */
    public static boolean isLogin() {
        return null != UserModel.getCurrentUser();
    }

    /**
     * 注销
     */
    public static void logout() {
        //注销用户系统
        BmobUser.logOut();
        //断开与im服务器的链接
        BmobIM.getInstance().disConnect();
    }

    /**
     * 获取当前用户信息
     *
     * @return
     */
    public static UserModel getCurrentUser() {
        return BmobUser.getCurrentUser(UserModel.class);
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static String getCurrentUserId() {
        return BmobUser.getCurrentUser().getObjectId();
    }

    /**
     * 获取当前即时通讯用户信息
     *
     * @return
     */
    public static BmobIMUserInfo getCurrentIMUserInfo() {
        return getIMUserInfo(getCurrentUser().getObjectId());
    }

    /**
     * 获取即时通讯用户信息
     *
     * @param objectId
     * @return
     */
    public static BmobIMUserInfo getIMUserInfo(String objectId) {
        return BmobIM.getInstance()
                .getUserInfo(objectId);
    }

    /**
     * 更新im用户信息
     *
     * @param userModel
     */
    public static void updateIMUserInfo(UserModel userModel) {
        updateIMUserInfo(parseUserInfo(userModel));
    }

    /**
     * 将用户对象转为im的用户对象
     *
     * @param userModel
     * @return
     */
    public static BmobIMUserInfo parseUserInfo(UserModel userModel) {
        BmobIMUserInfo userInfo = new BmobIMUserInfo();
        userInfo.setUserId(userModel.getObjectId());
        userInfo.setName(userModel.getNickname());
        userInfo.setAvatar(userModel.getAvatar());
        return userInfo;
    }

    /**
     * 更新im用户信息
     *
     * @param userInfo
     */
    public static void updateIMUserInfo(BmobIMUserInfo userInfo) {
        BmobIM.getInstance().updateUserInfo(userInfo);
    }
}